package com.sky;

/**
 * @author：sky-吴 日期：2020/4/27
 * 邮箱:
 * 牛逼可以写一个算法, 实现json tree的解析
 */

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;



public class JacksonTester {
	public static void main(String args[]) {


		//创建一个 ObjectMapper 对象出来, 准备读取json
		ObjectMapper mapper = new ObjectMapper();
		String jsonString = "{\"name\":\"Mahesh Kumar\", " +
				"\"age\":21,\"verified\":false,\"marks\": [100,90,85]}";
		//创建一个jsonNode 对象出来解析这个json
		JsonNode rootNode = null;
		try {
			rootNode = mapper.readTree(jsonString);

			JsonNode nameNode = rootNode.get("name");
			System.out.println("Name: " + nameNode.textValue());

			JsonNode ageNode = rootNode.path("age");
			System.out.println("Age: " + ageNode.intValue());

			JsonNode verifiedNode = rootNode.path("verified");
			System.out.println("Verified: " + (verifiedNode.asBoolean() ? "Yes" : "No"));

			JsonNode marksNode = rootNode.path("marks");
			JsonNode tempNode = null;
			System.out.print("Marks: [ ");
			for (int i = 0; i < marksNode.size(); i++) {
				tempNode = marksNode.get(i);
				System.out.print(tempNode.asInt() + " ");
			}
			System.out.println("]");
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

	}
}
/*
输出：
		Name: Mahesh Kumar
		Age: 21
		Verified: No
		Marks: [ 100 90 85 ]
 */
